package presentation;


import com.cloudgarden.layout.AnchorLayout;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.border.LineBorder;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PostView extends javax.swing.JPanel {

	private static final long serialVersionUID = 3251456157061972776L;
	private JTextPane _content;
	private JLabel _owner;
	private JLabel _subject;

	public static final int FONT_SIZE = 11;
	//TODO : change to real Size
	public static final int ROW_WIDTH = 100;
	public static final int ROW_HEIGHT = FONT_SIZE + 3;
	private JLabel score;
	private JLabel postTime;
	private JButton deleteButton;
	private JButton modifyButton;
	private int postIndex; //index in thread of posts
	private WsepGUI container; // the GUI inside which this post was created


	public PostView(WsepGUI gui, int index, String title, String owner, String content, String time, boolean modify, boolean delete) {
		super();
		initGUI(title, owner, content, time);
		this.modifyButton.setVisible(modify);
		this.deleteButton.setVisible(delete);
		this.postIndex = index;
		this.container = gui;
	}

	private void initGUI(String title, String owner, String content, String time) {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			setPreferredSize(new Dimension(ROW_WIDTH, numOfRows(content) * ROW_HEIGHT + 30));
			thisLayout.rowWeights = new double[] {0.0, 0.1, 0.0};
			thisLayout.rowHeights = new int[] {19, 7, 16};
			thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
			thisLayout.columnWidths = new int[] {7, 7, 7, 7};
			this.setLayout(thisLayout);
			this.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, true));
			{
				_content = new JTextPane();
				this.add(_content, new GridBagConstraints(0, 1, 4, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				_content.setFont(new java.awt.Font("Arial",0,FONT_SIZE));
				_content.setText(content);
				_content.setEditable(false);
			}
			{
				_subject = new JLabel();
				@SuppressWarnings("unused")
				AnchorLayout _subjectLayout = new AnchorLayout();
				this.add(_subject, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				_subject.setText(title);
				_subject.setLayout(null);
			}
			{
				_owner = new JLabel();
				this.add(_owner, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				_owner.setText("By " + owner);
			}
			{
				modifyButton = new JButton();
				this.add(getDButton(), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				modifyButton.setText("Modify");
				modifyButton.setFont(new java.awt.Font("Tahoma",1,9));
				modifyButton.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						handleModify();
					}
				});
			}
			{
				deleteButton = new JButton();
				this.add(getMButton(), new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				deleteButton.setText("Delete");
				deleteButton.setFont(new java.awt.Font("Tahoma",1,9));
				deleteButton.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						handleDelete();
					}
				});
			}
			{
				//score = new JLabel();
				//this.add(getScore(), new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				//postTime.setText("Score:     ");
				//postTime.setFont(new java.awt.Font("Tahoma",1,11));		
				
				postTime = new JLabel();
				this.add(getPostTime(), new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				postTime.setText(time);
				postTime.setFont(new java.awt.Font("Tahoma",1,11));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void addScore(String scr) {
		score = new JLabel();
		this.add(getScore(), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		score.setText("Score:" + scr);
		score.setFont(new java.awt.Font("Tahoma",1,11));		
	}
	
	private void handleModify() {
		this.container.startModify(this,getPostIndex());
	}


	private void handleDelete() {
		int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this Post\n"+ "\""+this.getSubject().getText()+"\"?", "Warning!", JOptionPane.YES_NO_OPTION);
		if(confirm == JOptionPane.YES_OPTION){
			this.container.HandleDeletePost(this,getPostIndex());
		}
	}



	private static int numOfRows(String msg) {
		return msg.split("\n").length + 1;
	}

	public JButton getDButton() {
		return modifyButton;
	}

	public JButton getMButton() {
		return deleteButton;
	}

	public int getPostIndex() {
		return postIndex;
	}

	public WsepGUI getContainer() {
		return container;
	}

	public JTextPane getContent() {
		return _content;
	}

	public JLabel getOwner() {
		return _owner;
	}

	public JLabel getSubject() {
		return _subject;
	}
	
	public JLabel getPostTime() {
		return postTime;
	}
	
	public JLabel getScore() {
		return score;
	}

}
